package com.aidisp.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;

/**
 * 字符串常用类
 * 
 * @author Z.x.Y
 * 
 */
public class StringUtil {
	/**
	 * MD5方式编码
	 * @param value
	 * @return
	 */
	public static String md5(String value) {
		if (value == null)
			value = "";
		String s = null;
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(value.getBytes());
			byte tmp[] = md.digest();
			char str[] = new char[16 * 2];
			int k = 0;
			for (int i = 0; i < 16; i++) {
				byte byte0 = tmp[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			s = new String(str);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return s;
	}
	/**
	 * 获取相应长度的随机ASCII码字符串
	 * @param length
	 * @return
	 */
	public static String randomAscii(int length){
		String value = "";
		if(length<1){
			return value;
		}
		for(int i=0;i<length;i++){
			value+=(char)randomNumber(0,127);
		}
		return value;
	}
	/**
	 * BASE64方式解码
	 * @param value
	 * @return
	 */
	public static String decodeBase64(String value) {
		byte[] bytes = null;
		Base64 decoder = new Base64();
		bytes = decoder.decode(value);
		return new String(bytes);
	}
	/**
	 * BASE64方式编码
	 * @param value
	 * @return
	 */
	public static String encodeBase64(String value){
		return new String(Base64.encodeBase64String(value.getBytes()));
	}
	public static int randomNumber(int min,int max){
		if(min==max){
			return min;
		}
		return (int)(Math.random()*(max-min+1))+min;
	}
	public static void main(String[] args) {
		System.out.println("java_vendor:" + System.getProperty("java.vendor"));     
	    System.out.println("java_vendor_url:"     
	             + System.getProperty("java.vendor.url"));     
	    System.out.println("java_home:" + System.getProperty("java.home"));     
	    System.out.println("java_class_version:"     
	             + System.getProperty("java.class.version"));     
	    System.out.println("java_class_path:"     
	            + System.getProperty("java.class.path"));     
	    System.out.println("os_name:" + System.getProperty("os.name"));     
	    System.out.println("os_arch:" + System.getProperty("os.arch"));     
	    System.out.println("os_version:" + System.getProperty("os.version"));     
	    System.out.println("user_name:" + System.getProperty("user.name"));     
	    System.out.println("user_home:" + System.getProperty("user.home"));     
	    System.out.println("user_dir:" + System.getProperty("user.dir"));     
	    System.out.println("java_vm_specification_version:"     
	            + System.getProperty("java.vm.specification.version"));     
	    System.out.println("java_vm_specification_vendor:"     
	            + System.getProperty("java.vm.specification.vendor"));     
	    System.out.println("java_vm_specification_name:"     
	            + System.getProperty("java.vm.specification.name"));     
	    System.out.println("java_vm_version:"     
	            + System.getProperty("java.vm.version"));     
	    System.out.println("java_vm_vendor:"     
	            + System.getProperty("java.vm.vendor"));     
	    System.out     
	            .println("java_vm_name:" + System.getProperty("java.vm.name"));     
	    System.out.println("java_ext_dirs:"     
	            + System.getProperty("java.ext.dirs"));     
	    System.out.println("file_separator:"     
	            + System.getProperty("file.separator"));     
	    System.out.println("path_separator:"     
	            + System.getProperty("path.separator"));     
	    System.out.println("line_separator:"     
	            + System.getProperty("line.separator")); 
	}
}
